package com.woniuxy.caradmin.controller;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.User;

import com.woniuxy.caradmin.entity.vo.UserVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.UserService;
import com.woniuxy.caradmin.util.Constaint;

import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/selectAllUser")
    @ApiOperation("查询所有用户")
    public R selectAllUser(){
        List<User> users = userService.selectAllUser();
        if(users !=null &&users.size()>0){
            return R.ok().data("users",users);
        }else {
            throw new CarException(ResultCode.USERISNULL,Constaint.USERISNULL);
        }
    }

    @GetMapping("/userList")
    @ApiOperation("查询所有用户")
    public DataGridView userList(UserVo useVo){
        Page<Object> page = PageHelper.startPage(useVo.getPage(), useVo.getLimit());
        List<User> list = userService.selectUserByPage(useVo);
        System.out.println("////"+list);
        return new  DataGridView(page.getTotal(),list);
    }

    @PostMapping("/delectUser")
    @ApiOperation("删除用户")
    public R delectUser(Integer id){
        Integer integer = userService.delectUser(id);
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @PostMapping("/insertUser")
    @ApiOperation("增加用户")
    public R insertUser(User user){
        Integer integer = userService.insertUser(user);
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @PutMapping("/updateUser")
    @ApiOperation("修改用户")
    public R updateUser(User user){
        Integer integer = userService.updateUser(user);
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @GetMapping("/selectPhoAndName")
    @ApiOperation("电话和名字搜索用户")
    public R selectPhoAndName(User user){
        List<User> users = userService.selectPhoAndName(user);
        if(users !=null &&users.size()>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }

    @RequestMapping("/assignRole")
    @ApiOperation("重新分配角色")
    public R assignRole(Integer[] rids,Integer uid){
        boolean flag = this.userService.assignRole(rids, uid);
        if(flag){
            return R.ok().code(ResultCode.USERNOTISNULL).message(Constaint.USERNOTISNULL);
        }else{
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }

    @ApiOperation("通过页面增加用户")
    @PostMapping("/useradds")
    public R useradd(HttpServletRequest request){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        String name = request.getParameter("name");
        String telephone = request.getParameter("telephone");
        String birthday = request.getParameter("birthday");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String dept = request.getParameter("dept");
        String entry = request.getParameter("entry");
        String statues = request.getParameter("statues");
        String available = request.getParameter("available");
        String positionid = request.getParameter("positionid");

            Date bir=null;
            Date ent=null;
            try {
                bir=sdf.parse(birthday);
                ent=sdf.parse(entry);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            java.sql.Date birdate=new java.sql.Date(bir.getTime());
            java.sql.Date entdate=new java.sql.Date(ent.getTime());
            User user=new User();
            user.setName(name);
            user.setTelephone(telephone);
            user.setBirthday(birdate);
            user.setPassword(password);
            user.setSex(sex);
            user.setDid(Integer.parseInt(dept));
            user.setEntry(entdate);
            user.setStatues(Integer.parseInt(statues));
            user.setAvailable(available);
            user.setPositionid(Integer.parseInt(positionid));
            user.setGradeid(null);
            System.out.println("+++++++++");
            Integer integer = userService.insertUser(user);
            if(integer>0){
                return R.ok().code(ResultCode.USERADDSUCCESS).message(Constaint.USERADDSUCCESS);
            }else {
                throw new CarException(ResultCode.USERADDFILTER,Constaint.USERADDFILTER);
            }
    }
    @ApiOperation("通过id查询用户")
    @PostMapping("/userByid")
    public R userByid(Integer id){
        User user = userService.selectUserByid(id);
        if(user !=null){
            return R.ok().code(ResultCode.USERNOTISNULL).message(Constaint.USERNOTISNULL);
        }else {
            throw new CarException(ResultCode.USERISNULL,Constaint.USERISNULL);
        }
    }
    @ApiOperation("界面用户修改")
    @PostMapping("/updUser")
    public R updUser(HttpServletRequest request){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String id=request.getParameter("uid");
        System.out.println("****"+id);
        String name = request.getParameter("name");
        String telephone = request.getParameter("telephone");
        String birthday = request.getParameter("birthday");
        String sex = request.getParameter("sex");
        String dept = request.getParameter("dept");
        String entry = request.getParameter("entry");
        String statues = request.getParameter("statues");
        String available = request.getParameter("available");
        String positionid = request.getParameter("positionid");

        Date bir=null;
        Date ent=null;
        try {
            bir=sdf.parse(birthday);
            ent=sdf.parse(entry);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        java.sql.Date birdate=new java.sql.Date(bir.getTime());
        java.sql.Date entdate=new java.sql.Date(ent.getTime());
        User user=new User();
        user.setId(Integer.parseInt(id));
        user.setName(name);
        user.setTelephone(telephone);
        user.setBirthday(birdate);
        user.setSex(sex);
        user.setDid(Integer.parseInt(dept));
        user.setEntry(entdate);

        System.out.println("statues"+statues);
        user.setStatues(Integer.parseInt(statues));
        user.setAvailable(available);
        user.setPositionid(Integer.parseInt(positionid));
        user.setGradeid(null);
        Integer integer = userService.updateUser(user);
        log.debug("22"+integer);
        if (integer>0){
            return R.ok().code(ResultCode.USERUPDATESUCCESS).message(Constaint.USERUPDATEFILTER);
        }else {
            throw new CarException(ResultCode.USERUPDATEFILTER,Constaint.USERUPDATEFILTER);
        }
    }
    @ApiOperation("界面用户删除")
    @PostMapping("/dellUser{id}")
    public R deleUser(@PathVariable("id") String id){
        Integer integer = userService.delectUser(Integer.parseInt(id));
        if(integer>0){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }else {
            throw new CarException(ResultCode.ERROR,Constaint.ERROR);
        }
    }
    @ApiOperation("批量删除")
    @PostMapping("/deleteAllPath")
    public R deleteAllPath(Integer[] ids){
        boolean b = userService.removePath(ids);
        if (b){
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        }
        return null;
    }

}

